import commands
import nmap
import csv
a = commands.getoutput("/sbin/ifconfig").split("\n")[1].split()[1][5:]
#a = "192.37.14.21"
nm = nmap.PortScanner()
# filtro para o range do ip 0.0.0.*
for x in range(4):
	b = a[len(a)-x-1:len(a)-x]
	if b == '.':
		break

b = a[:len(a)-x]+'*'

nm.scan(hosts = b, ports='21-443', arguments ='-sV -sS -O')
#csv = nm.csv()
hosts = nm.all_hosts()
portas = nmap.PortScannerAsync()
status = nm.scanstats()
scaninfo = nm.scaninfo()

#print csv
#print hosts
#print port
#print status
#print scaninfo
'''
for x in hosts:
	print x
	print port
'''

for host in nm.all_hosts():
     print('----------------------------------------------------')
     print('Host : %s (%s)' % (host, nm[host].hostname()))
     print('State : %s' % nm[host].state())
     try:
     	osversion = nm[host]['osclass']
     	#print 'Sistema Operativo -->',osversion
     	print '\nSistema Operativo --> {1}{3}'.format(get(nm[host]['osclass']['vendor']),get(nm[host]['osclass']['osgen']))
     except:
	pass
     for proto in nm[host].all_protocols():
         if proto == 'tcp':
		 print('----------')
		 print('Protocol : %s' % proto)
		 print proto
		 try:
	 	    lport = nm[host]['tcp'].keys()
		    lport.sort()
		 except:
		    pass
		 for port in lport:
		     try:
		        print 'port:',port,' State:',nm[host]['tcp'][port]['state']
			print ' Services',nm[host]['tcp'][port],'\n'
		     except:
			pass
	 else:
		 pass

